<template>
	<view class="content">
		<view class="">当前设备累积用电量：{{ totalDu || '--' }}度</view>
		<view class="">当前设备电量：{{ textDu || '--' }}度</view>
		<view class="">当前设备损耗：{{ textSpeed ? textSpeed : '--' }}%</view>
		<view class="">当前设备功率：{{ power }}</view>
		<view class="">当前设备状态：{{ status == 1 ? '开闸' : '关闸' }}</view>

		<view class="ipt">
			<text>请输入表mac地址：</text>
			<input type="text" placeholder="输入mac地址" v-model="mac" />
		</view>
		<view class="ipt">
			<text>请输入充值度数：</text>
			<input type="digit" placeholder="输入充值度数" v-model="du" />
			<text>度</text>
		</view>
		<view class="ipt">
			<text>请输入损耗：</text>
			<input type="text" placeholder="输入损耗" v-model="speed" />
			<text>%</text>
		</view>
		<button @click="connectDevice">连接</button>
		<button @click="openGate">开闸</button>
		<button @click="closeGate">关闸</button>
		<button @click="recharge">电量充值</button>
		<button @click="readMeter">读表</button>
		<button @click="clearZero">清零</button>
		<button @click="readLoss">读取损耗</button>
		<button @click="setLoss">设置损耗</button>
	</view>
</template>

<script>
import { searchDevice } from '../../util/device.js';
import { openOrCloseBleDevice, rechargeBleDevice, queryBleDevice, clear, redSpeed, setSpeed } from '../../util/device_action.js';
export default {
	onShareAppMessage(res) {
		if (res.from === 'button') {
			// 来自页面内分享按钮
			console.log(res.target);
		}
		return {
			title: 'demo',
			path: '/pages/index/index'
		};
	},
	data() {
		return {
			title: 'Hello',
			mac: '',
			du: '',
			speed: '',
			totalDu: '',
			power: '',
			status: '',
			textDu: '',
			textSpeed: ''
		};
	},
	onLoad() {},
	methods: {
		verify() {
			return new Promise((resolve, reject) => {
				if (!this.mac) {
					uni.showToast({
						title: '请输入mac地址',
						icon: 'none'
					});
					reject();
				} else {
					resolve();
				}
			});
		},
		// 连接设备
		async connectDevice() {
			await this.verify();
			await searchDevice({
				mac: this.mac,
				isAutoConnect: true
			});
			uni.showToast({
				title: '连接成功',
				icon: 'none'
			});
		},
		// 开闸
		async openGate() {
			await this.verify();
			await openOrCloseBleDevice({
				status: 1
			});
		},
		// 关闸
		async closeGate() {
			await this.verify();
			await openOrCloseBleDevice({
				status: 0
			});
		},
		// 充值
		async recharge() {
			await this.verify();
			if (!this.du) {
				return uni.showToast({
					title: '请输入度数',
					icon: 'none'
				});
			}
			const res = await queryBleDevice();
			//需要先读取度数然后累加再触发充值
			this.du = parseFloat(res.du) + parseFloat(this.du);

			await rechargeBleDevice({
				du: this.du.toFixed(3)
			});
			uni.showToast({
				title: '充值电量成功',
				icon: 'success'
			});
		},
		// 读表
		async readMeter() {
			await this.verify();
			const res = await queryBleDevice();
			this.textDu = res.du;
			this.totalDu = res.totalDu;
			this.power = res.power;
			this.status = res.status;
		},
		// 清零
		async clearZero() {
			await this.verify();
			await clear();
			uni.showToast({
				title: '清零成功',
				icon: 'success'
			});
		},
		// 读取损耗
		async readLoss() {
			await this.verify();
			const res = await redSpeed();
			// 真实数据是是*1000，展示的是百分比，所以除以10即可
			this.textSpeed = res.speed / 10;
		},
		// 设置损耗
		async setLoss() {
			await this.verify();
			if (!this.speed) {
				return uni.showToast({
					title: '请输入损耗',
					icon: 'none'
				});
			}
			// 因为展示的是百分比，真实的需要/100
			await setSpeed({
				speed: this.speed / 100
			});
		}
	}
};
</script>

<style lang="less">
.content {
	.ipt {
		display: flex;
	}
}
</style>